package array;

/**
 * @ClassName climbStairs
 * @Description TODO
 * @Author lenovo
 * @Date 2023-02-09 16:08
 * @Version 1.0
 * @Comment Magic. Do not touch.
 * If this comment is removed. the program will blow up
 */
public class ClimbStairs_01 {

    /**
     * 题目描述：假设你正在爬楼梯。需要 n 阶你才能到达楼顶。
     * <p>
     * 每次你可以爬 1 或 2 个台阶。你有多少种不同的方法可以爬到楼顶呢？
     * <p>
     * 注意：给定 n 是一个正整数。
     *
     * @param n
     * @return 递归
     */
    public static int climbStairs(int n) {

        if (n == 1 || n == 2) {
            return n;
        }
        return climbStairs(n - 1) + climbStairs(n - 2);
    }

    /**
     * 迭代
     * @param n
     * @return
     */
    public static int climbStairs1(int n) {
        if (n == 1 || n == 2) {
            return n;
        }

        int first = 1, second = 2, result = -1;
        for (int i = 3; i <= n; i++) {
            result = first + second;
            first = second;
            second = result;
        }
        return result;
    }

    public static void main(String[] args) {

        System.out.println(climbStairs(5));
        System.out.println(climbStairs1(5));
    }
}